package com.ssd.controller;


import com.ssd.api.user.dto.AddressBook;
import com.ssd.service.IAddressBookService;
import com.ssd.utils.UserContext;
import com.ssd.view.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 地址薄 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-11-14
 */
@RestController
@RequestMapping("/address-book")
@Api(tags = "地址薄 前端控制器")
public class AddressBookController {
    @Resource
    private IAddressBookService addressBookService;


    /**
     * 新增地址信息
     * @param addressBook 地址信息
     * @return
     */
    @PostMapping
    @ApiOperation(value = "添加地址")
    public BaseResponse addAddressBook(@RequestBody AddressBook addressBook){
        return addressBookService.addAddressBook(addressBook);
    }


    /**
     * 查询当前登录用户的所有地址信息
     * @return 地址信息
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询当前登录用户的所有地址信息")
    public BaseResponse<List<AddressBook>> getAddressBookList(){
        return new BaseResponse<>(addressBookService.lambdaQuery()
                .eq(AddressBook::getUserId, UserContext.currentUser().getUserId())
                .list());
    }

    /**
     * 查询当前登录用户的默认地址
     * @return 地址信息
     */
    @GetMapping("/default")
    @ApiOperation("查询当前登录用户的默认地址")
    public AddressBook getDefault(){
        return addressBookService.getDefault();
    }

    /**
     * 根据id修改地址
     * @param addressBook 地址信息
     * @return
     */
    @PutMapping
    @ApiOperation(value = "根据id修改地址")
    public BaseResponse putAddressBook(@RequestBody @Validated AddressBook addressBook){
    	return addressBookService.putAddressBook(addressBook);
    }

    /**
     * 根据id删除地址
     * @param id 地址id
     * @return
     */
    @DeleteMapping
    @ApiOperation(value = "根据id删除地址")
    public BaseResponse deleteAddressBook(@RequestParam("id") Long id){
        return new BaseResponse(addressBookService.removeById(id));
    }

    /**
     * 根据地址id查询地址
     * @param id 地址id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询地址")
    public BaseResponse<AddressBook> getAddressBookById(@PathVariable("id")Long id){
    	return new BaseResponse<>(addressBookService.getById(id));
    }

    /**
     * 根据地址id设置默认地址
     * @param addressBook 地址id
     * @return
     */
    @PutMapping("/default")
    @ApiOperation(value = "设置默认地址")
    public BaseResponse putDefault(@RequestBody AddressBook addressBook){
    	return addressBookService.putDefault(addressBook);
    }
}
